/******************************************************************
 * Created by: GOh CHee Hong                                      
 * Date of Creation: 1/Jan/2009            
 
 *                                                                
 * Description: This class is in charge of managing the subscription
 ******************************************************************/
/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Pbdms.Common.DataObject;
using System.Windows.Forms;
namespace Pbdms.BusinessLogic.RSS
{
    public class SubscriptionManager
    {
        public Hashtable arrSubscriptions;
        private BusinessLogic.Network.NetworkController networkCtrller ;
        //event handlers
        public event EventHandler Changed;
        public event EventHandler NewFeeds;
        public event EventHandler SubscriptionRemoved;
        public event EventHandler SubscriptionAdded;
        //source event handler
        public event EventHandler RefreshStart;
        public event EventHandler RefreshDone;
        public delegate void RefreshProcessHandler(object sender, string args);
        public RefreshProcessHandler RefreshProcess;
        public event EventHandler RefreshFailed;
        public SubscriptionManager( BusinessLogic.Network.NetworkController networkCtrller)
        {
            arrSubscriptions = new Hashtable();
            this.networkCtrller = networkCtrller;
        }
        //load all subscriptions
        public void loadSubscription()
        {
            //set up every subscription
            ArrayList SubscrsInfos = BusinessLogic.DataController.SubscriptionInfoController.create().getListOfSubscriptionsInfo();
            foreach(SubscriptionInfo subscrInfo in SubscrsInfos){
                Subscription subscription = new Subscription(subscrInfo, networkCtrller );
                  
               
                arrSubscriptions.Add(subscription.cacheKey,subscription);
                //signal the tree add subscription event
                if (SubscriptionAdded != null)
                {
                    SubscriptionAdded(subscription, EventArgs.Empty);
                }
                setUpEventHandling(subscription);
                
            }

        }
        public void saveFeedsToCache()
        {

            // Only save feeds if there are any subscriptions
            IEnumerator enumerator = arrSubscriptions.Values.GetEnumerator();
            if (enumerator.MoveNext())
            {
                Subscription subscription = (Subscription)enumerator.Current;
                subscription.saveCompleteFeedCache();

            }
            else
            {

            }


        }

        //add subscription event
        public void addSubscription(SubscriptionInfo subscrInfo)
        {
            Subscription subscription = new Subscription(subscrInfo, networkCtrller);
            arrSubscriptions.Add(subscription.cacheKey, subscription);
            //set up event
            setUpEventHandling(subscription);
            if (SubscriptionAdded != null)
            {
                SubscriptionAdded(subscription, EventArgs.Empty);
            }
        }
        //add subscription event
        public void addSubscription(string channelName,string keyID)
        {
            Common.DataObject.SubscriptionInfo subscrInfo = BusinessLogic.DataController.SubscriptionInfoController.create().appendSubscriptionInfoToDB(channelName, keyID);
            Subscription subscription = new Subscription(subscrInfo,networkCtrller);
            arrSubscriptions.Add(subscription.cacheKey,subscription);
            //set up event
            if (SubscriptionAdded != null)
            {
                SubscriptionAdded(subscription, EventArgs.Empty);
            }
            setUpEventHandling(subscription);
        }
        //remove a subscription
        public void removeSubscription(Subscription subscr)
        {
            BusinessLogic.DataController.SubscriptionInfoController.create().deleteSubscriptionInfoFromDB(subscr.info);
            Subscription subscription= (Subscription) arrSubscriptions[subscr.cacheKey];
            subscription.feedManager.clearCache();
            
            arrSubscriptions.Remove(subscr.cacheKey);
            removeEventHandling(subscr);
            if (SubscriptionRemoved != null)
            {
                //signal the Gui handler
                SubscriptionRemoved(subscr, EventArgs.Empty);
                System.IO.File.Delete(Common.Global.getTempFolder() + "\\EFC258B3FA75F857 Newest Library additions");
            }   
        }

        //event handling per subscription
        private void setUpEventHandling(Subscription subscr)
        {
           
            subscr.mFeedManager.NewFeeds += new EventHandler(OnNewFeeds);
            subscr.mFeedManager.RefreshDone += new EventHandler(OnRefreshDone);
            subscr.mFeedManager.RefreshFailed += new EventHandler(OnRefreshFailed);
            subscr.mFeedManager.RefreshStart += new EventHandler(OnRefreshStart);
            subscr.mFeedManager.RefreshProcess += new RefreshProcessHandler(OnRefreshProcess);
        }
        //event handling for all
        private void setUpAllEventHandling()
        {
            foreach (Subscription subscription in arrSubscriptions)
            {
                setUpEventHandling(subscription);
            }

        }
        //remove event handling of one subscription
        private void removeEventHandling(Subscription subscr)
        {
            subscr.mFeedManager.NewFeeds -= new EventHandler(OnNewFeeds);
            subscr.mFeedManager.RefreshDone -= new EventHandler(OnRefreshDone);
            subscr.mFeedManager.RefreshFailed -= new EventHandler(OnRefreshFailed);
            subscr.mFeedManager.RefreshStart -= new EventHandler(OnRefreshStart);
            subscr.mFeedManager.RefreshProcess -= new RefreshProcessHandler(OnRefreshProcess);
        }
        //remove all event handling 
        private void removeAllEventHandling()
        {
            foreach (Subscription subscription in arrSubscriptions)
            {
                removeEventHandling(subscription);
            }
        }
        //new feeds
        private void OnNewFeeds(object source, EventArgs arg)
        {
            if (NewFeeds != null)
            {
                NewFeeds(source, arg);
            }
        }
        private void OnRefreshDone(object source, EventArgs arg)
        {
            if (RefreshDone != null)
            {
                RefreshDone(source, arg);
            }
        }
        private void OnRefreshFailed(object source, EventArgs arg)
        {
            if (RefreshFailed != null)
            {
                RefreshFailed(source, arg);
            }
        }
        private void OnRefreshStart(object source, EventArgs arg)
        {
            if (RefreshStart != null)
            {
                RefreshStart(source, arg);
            }
        }
        private void OnRefreshProcess(object source, string percentage)
        {
            if (RefreshProcess != null)
            {
                RefreshProcess(source,percentage);
            }
        }
        public void RefreshAll()
        {
            foreach (Subscription subscription in arrSubscriptions.Values)
            {
                subscription.feedManager.refreshFeeds();
            }
        }
        //handle incoming feed download starts event

        
       
    }
}
